package io.renren.modules.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.health.dao.ArticlesDao;
import io.renren.modules.health.dto.AppIndexDTO;
import io.renren.modules.health.dto.ArticlesDTO;
import io.renren.modules.health.dto.ArticlesTypeDTO;
import io.renren.modules.health.entity.ArticlesEntity;
import io.renren.modules.health.service.ArticlesService;
import io.renren.modules.health.service.ArticlesTypeService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 资讯内容表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-05-09
 */
@Service
public class ArticlesServiceImpl extends CrudServiceImpl<ArticlesDao, ArticlesEntity, ArticlesDTO> implements ArticlesService {

    @Autowired
    private ArticlesTypeService articlesTypeService;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public QueryWrapper<ArticlesEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String title = (String)params.get("title");
        String searchKeywords = (String)params.get("searchKeywords");
        String articlesTypeId = (String)params.get("articlesTypeId");
        Object creator = params.get("creator");

        QueryWrapper<ArticlesEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.like(StringUtils.isNotBlank(title), "title", title);
        wrapper.like(StringUtils.isNotBlank(searchKeywords), "search_keywords", searchKeywords);
        wrapper.like(StringUtils.isNotBlank(articlesTypeId), "articles_type_id", articlesTypeId);
        if (creator != null) {
            wrapper.eq("creator", Long.valueOf(creator.toString()));
        }
        return wrapper;
    }


    @Override
    public PageData<ArticlesDTO> pageArticles(Map<String, Object> params) {
        // 获取当前登录用户
        Long userId = SecurityUser.getUserId();
        params.put("creator", userId);
        // 获取所有的资讯类型
        Map<String, Object> map = new HashMap<>();
        List<ArticlesTypeDTO> articlesTypeDtoList = articlesTypeService.list(map);

        PageData<ArticlesDTO> page = this.page(params);
        page.getList().forEach(e -> {
            ArticlesTypeDTO articlesTypeDto = articlesTypeDtoList.stream().filter(type -> type.getId().compareTo(e.getArticlesTypeId()) == 0).findAny().orElse(null);
            e.setArticlesTypeName(articlesTypeDto == null ? "" : articlesTypeDto.getTypeName());

            SysUserDTO creatorDto = sysUserService.get(e.getCreator());
            e.setCreatorName(creatorDto == null ? "" : creatorDto.getUsername());
            SysUserDTO updaterDto = sysUserService.get(e.getUpdater());
            e.setUpdaterName(creatorDto == null ? "" : updaterDto.getUsername());
        });
        return page;
    }

    @Override
    public List<AppIndexDTO> appIndexList(Map<String, Object> params) {
        QueryWrapper<ArticlesEntity> wrapper = new QueryWrapper<>();
        wrapper.like("status", 1);
        wrapper.like("index_status", 1);
        List<ArticlesEntity> entityList = this.baseDao.selectList(wrapper);
        List<ArticlesDTO> dtoList = ConvertUtils.sourceToTarget(entityList, ArticlesDTO.class);
        Map<Long, List<ArticlesDTO>> typeArticlesMap = dtoList.stream().collect(Collectors.groupingBy(ArticlesDTO::getArticlesTypeId));
        List<AppIndexDTO> appIndexList = new ArrayList<>();
        typeArticlesMap.keySet().forEach(typeId -> {
            ArticlesTypeDTO articlesTypeDTO = articlesTypeService.get(typeId);
            List<ArticlesDTO> articlesDtoList = typeArticlesMap.get(typeId);
            if (articlesTypeDTO != null) {
                AppIndexDTO appIndex = new AppIndexDTO();
                appIndex.setArticlesTypeId(articlesTypeDTO.getId());
                appIndex.setArticlesTypeName(articlesTypeDTO.getTypeName());
                appIndex.setArticlesDtoList(articlesDtoList);
                appIndexList.add(appIndex);
            }
        });
        return appIndexList;
    }

    @Override
    public List<ArticlesDTO> listArticles(Map<String, Object> params) {
        // 获取所有的资讯类型
        Map<String, Object> map = new HashMap<>();
        List<ArticlesTypeDTO> articlesTypeDtoList = articlesTypeService.list(map);
        List<ArticlesDTO> list = this.list(params);
        list.forEach(e -> {
            ArticlesTypeDTO articlesTypeDto = articlesTypeDtoList.stream().filter(type -> type.getId().compareTo(e.getArticlesTypeId()) == 0).findAny().orElse(null);
            e.setArticlesTypeName(articlesTypeDto == null ? "" : articlesTypeDto.getTypeName());

            SysUserDTO creatorDto = sysUserService.get(e.getCreator());
            e.setCreatorName(creatorDto == null ? "" : creatorDto.getUsername());
            SysUserDTO updaterDto = sysUserService.get(e.getUpdater());
            e.setUpdaterName(creatorDto == null ? "" : updaterDto.getUsername());
        });
        return list;
    }
}